package hzst.android.form.view;

import hzst.android.form.ViewCreator;
import hzst.android.form.info.BaseViewInfo;
import hzst.android.form.info.BaseViewInfo.SubmitValue;
import hzst.android.form.info.SelectableViewInfo;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class FMSpinner extends FMBaseView{
	private Spinner view;
	
	private List<String> dataList;
	
	public FMSpinner(Context context) {
		super(context);
	}
	
	@Override
	public void setInfo(BaseViewInfo info, ViewCreator viewCreator) {
		super.setInfo(info, viewCreator);
		dataList = ((SelectableViewInfo)info).getListText();
	}
	@Override
	public void showView() {
		view = new Spinner(context);
		ArrayAdapter<String> adapter=new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,dataList);
		((Spinner) view).setAdapter(adapter);
		
		final List<String> listValue = ((SelectableViewInfo)info).getListValue();
		String value = info.getSubmitValues().get(0).getValue();//下拉选只有一个value
		/*
		 * 遍历value，选中与默认值相同的下标的选项
		 */
		for (int i = 0; i < listValue.size(); i++) {
			if(value.equals(listValue.get(i))){
				((Spinner) view).setSelection(i);
				break;
			}
			if("".equals(value)){
				((Spinner) view).setSelection(0);
				break;
			}
		}
		
		if(info.isReadOnly()){
			view.setEnabled(false);
		}
		
		((Spinner) view).setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
//				BaseViewInfo info = viewList.get((int) parent.getTag());
				/*
				 * 当选中一项时，获取下标对应的value，并保存修改
				 */
				List<SubmitValue> submitValues = info.getSubmitValues();
				String value = listValue.get(position);
				if(submitValues.size() == 2){//当下拉选需要同时上传下标和值时
					if(BaseViewInfo.TYPE_SV.equals(submitValues.get(0).getValueType())){
						viewCreator.setSubmitValue(value, info, 0);
						viewCreator.setSubmitValue(((Spinner) parent).getSelectedItem().toString(), info, 1);
					}else{
						viewCreator.setSubmitValue(value, info, 1);
						viewCreator.setSubmitValue(((Spinner) parent).getSelectedItem().toString(), info, 0);
					}
				}
				viewCreator.setSubmitValue(value, info);
				((SelectableViewInfo)info).setPosition(position);
//				viewCreator.updateViewInfo(info, true);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				
			}
		});
		
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
		view.setLayoutParams(params);
		setLayoutParams(params);
		addView(view);
	}
	
	@Override
	public void setContent(CharSequence content) {
		for (int i = 0; i < dataList.size(); i++) {
			if(dataList.get(i).equals(content)){
				view.setSelection(i);
				break;
			}
		}
	}

}
